package com.obsidian.free.slideshow;

import android.app.ListActivity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SlideshowClickOptions extends ListActivity implements OnItemClickListener{
   int appWidgetId,size;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      appWidgetId = getIntent().getIntExtra("wid", AppWidgetManager.INVALID_APPWIDGET_ID);
      size = getIntent().getIntExtra("size", SlideshowUpdateService.SIZE_SMALL);
      
      StringAdapter adapter = new StringAdapter(this);
      setListAdapter(adapter);
      getListView().setOnItemClickListener(this);
   }
   
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      switch(position) {
         case 0:
            // next image
            Log.d("Slideshow", "Next image");
            ImageDatabase db = new ImageDatabase(this);
            db.invalidateWidget(appWidgetId);
            RemoteViews views = SlideshowUpdateService.buildUpdate(this, appWidgetId);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(appWidgetId, views);
            db.invalidateWidget(appWidgetId);
            db.close();
            finish();
            break;
         case 1:
            // configure
            Log.d("Slideshow", "Edit slideshow");
            Intent configIntent = new Intent(this, SlideshowConfigureImages.class);
            configIntent.putExtra("appWidgetId", appWidgetId);
            configIntent.putExtra("size", size);
            configIntent.putExtra("type", SlideshowConfigure.TYPE_LOCAL);
            configIntent.putExtra("edit", true);
            startActivity(configIntent);
            finish();
            break;
      }
   }
   
   private class StringAdapter extends BaseAdapter {
      String[] items = {
         getString(R.string.click_next),
         getString(R.string.click_edit)
      };
      
      Context mContext;
      LayoutInflater mInflate;
      public StringAdapter(Context context) {
         mContext = context;
         mInflate = LayoutInflater.from(context);
      }
      public int getCount() {
         return items.length;
      }
      public Object getItem(int position) {
         return items[position];
      }
      public long getItemId(int position) {
         return 0;
      }
      public View getView(int position, View convertView, ViewGroup parent) {
         TextView view = (TextView) mInflate.inflate(android.R.layout.simple_list_item_1, null);
         view.setText(items[position]);
         view.setTag(view);
         
         return view;
      }
   }
}
